﻿using System.Collections.ObjectModel;
using System.Windows;

using YourLastOptionsDialog.ViewModels.Options;

namespace YourLastOptionsDialog.ViewModels
{
    /// <summary>
    /// A view model for a single pivot item that contains a list of options sharing the same group name.
    /// </summary>
    public class OptionsItemViewModel
    {
        private readonly OptionGroup _optionGroup;

        /// <summary>
        /// Gets the header that should be used in the UI.
        /// </summary>
        public string Header
        {
            get
            {
                return _optionGroup.GroupName;
            }
        }

        /// <summary>
        /// Gets the options that are to be displayed in the UI.
        /// </summary>
        public ObservableCollection<FrameworkElement> Options
        {
            get;
            private set;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="OptionsItemViewModel"/> class.
        /// </summary>
        /// <param name="optionGroup">The option group to use for this item.</param>
        public OptionsItemViewModel(OptionGroup optionGroup)
        {
            Options = new ObservableCollection<FrameworkElement>();
            _optionGroup = optionGroup;
            CreateViews();
        }

        private void CreateViews()
        {
            Options.Clear();

            // instead of this code, we could use something fancy,
            // like a data template selector implementation
            // but this is simpler and also faster
            var viewFactory = OptionsService.Current.OptionsViewFactory;
            foreach (var option in _optionGroup.Options)
            {
                var view = viewFactory.CreateViewFor(option);
                if (view != null)
                {
                    Options.Add(view);
                }
            }
        }
    }
}